/*
依据题意模拟即可
*/
#include<iostream>
#include<string>
#include<string.h>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#include<cmath>
using namespace std;
#define int long long
#define pi acos(-1)

int n;
int a[5005][5005];
int b[5005][5005];
int temp[5005][5005];

inline int book()
{
	int cnt = 0;
	for(int i=1;i<=n;++i)
		for(int j=1;j<=n;++j)
			if(a[i][j]==b[i][j]) ++cnt;
	
	return cnt;
}
signed main()
{
	while(~scanf("%d",&n)&&n)
	{
		for(int i=1;i<=n;++i)
			for(int j=1;j<=n;++j)
				scanf("%d",&a[i][j]);
		
		for(int i=1;i<=n;++i)
			for(int j=1;j<=n;++j)
				scanf("%d",&b[i][j]);
		
		int ans = 0;
		ans = max(ans,book());
		for(int kk=1;kk<=3;++kk)
		{
			for(int i=1;i<=n;++i)
				for(int j=1;j<=n;++j)
					temp[i][j] = a[i][j];
			
			for(int i=1;i<=n;++i)
				for(int j=1;j<=n;++j)
					a[i][j] = temp[n-j+1][i];
			
			ans = max(ans,book());
		}
		
		printf("%d\n",ans);
	}
	return 0;
}
